<template>
  <div class="room">
    <!-- 顶部区域 -->
    <van-nav-bar
      :title="`聊天室(${userList.length})`"
      right-text="•••"
      left-arrow
      @click-left="backward()"
      @click-right="backward()"
    />
    <!-- 聊天区 -->
    <div class="chat" ref="chat">
      <ul>
        <li
          :class="item.username === userInfo.username ? 'right' : 'left'"
          v-for="(item, index) in messageList"
          :key="index"
        >
          <template v-if="item.username">
            <van-image
              class="avatar"
              round
              fit="cover"
              width="35px"
              height="35px"
              :src="require(`@/assets/images/${item.avatar}.jpeg`)"
            />
            <div class="info">
              <div class="username">{{ item.username }}</div>
              <div class="message">{{ item.message }}</div>
            </div>
          </template>
          <span class="span" v-else>{{ item.message }}</span>
        </li>
      </ul>
    </div>

    <!-- 底部区域 -->
    <div class="input">
      <van-icon name="audio" size="35" />
      <van-field v-model="message" placeholder="请输入" />
      <div class="right">
        <van-icon name="smile-o" size="35" />
        <van-button class="send" type="info" size="small" @click="send">发送</van-button>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'Room',
  data() {
    return {
      message: ''
    }
  },
  mounted() {},
  updated() {
    this.$nextTick(() => {
      this.$refs.chat.scrollTop = this.$refs.chat.scrollHeight
    })
  },
  computed: {
    ...mapState(['messageList', 'userInfo', 'userList'])
  },
  methods: {
    backward() {
      this.$socket.emit('outChatRoom')
      this.$router.back()
    },
    send() {
      this.$socket.emit('send', this.message)
      this.message = ''
    }
  }
}
</script>

<style scoped lang="less">
.room {
  width: 100%;
  height: 100vh;
  background-color: rgba(218, 216, 216, 0.801);
  .input {
    height: 54px;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    background-color: rgb(241, 240, 240);
    .van-icon {
      padding: 0 10px;
    }
    .right {
      display: flex;
      justify-content: center;
      align-items: center;
      .send {
        width: 50px;
        height: 35px;
        margin-right: 10px;
      }
    }
  }
  .chat {
    overflow-y: scroll;
    &::-webkit-scrollbar {
      display: none;
    }
    height: 85vh;
    margin-bottom: 54px;
    .left {
      display: flex;
      margin: 15px 10px;
      .username {
        font-size: 12px;
        color: rgb(110, 110, 110);
      }
      .message {
        max-width: 235px;
        font-size: 20px;
      }
      .avatar {
        margin-right: 15px;
      }
    }
    .right {
      display: flex;
      flex-direction: row-reverse;
      margin: 15px 10px;
      .info {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
      }
      .username {
        font-size: 12px;
        color: rgb(110, 110, 110);
      }
      .message {
        max-width: 235px;
        font-size: 20px;
      }
      .avatar {
        margin-left: 15px;
      }
    }
    .span {
      font-size: 14px;
      color: rgb(110, 110, 110);
      margin: 0 auto;
    }
  }
}
</style>
